﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace DeepEarth.Map.Core.Converters
{
    /// <summary>
    /// Bool to Visibility converter with parameter to invert.
    /// </summary>
    public class BooleanVisibilityConverter : IValueConverter
    {
        private static Visibility Convert(bool? value, bool? defaultValue)
        {
        	var visibility = value ?? (defaultValue ?? true);

        	return visibility ? Visibility.Visible : Visibility.Collapsed;
        }

    	private static bool ConvertBack(Visibility value)
        {
            return value == Visibility.Visible;
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
			if (targetType == typeof(Visibility))
				return Convert((bool?) value, (bool?) parameter);
        	
			if (targetType == typeof(Boolean))
        		return ConvertBack(value, targetType, true, culture);

			throw new ArgumentException("BooleanVisibilityConverter can only convert between boolean and visibility");
        }

    	public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ConvertBack((Visibility)value);
        }
    }
}
